package com.ruoyi.web.controller.business.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.web.controller.business.goods.factory.JbPackageFactory;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.goods.domain.JbPackageMain;
import com.ruoyi.business.goods.service.JbPackageMainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 套餐 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("套餐")
@RestController
@RequestMapping("/package_main")
public class JbPackageMainController extends BaseController
{
    @Autowired
    private JbPackageMainService service;
    @Autowired
    private JbPackageCommodityController jbPackageCommodityController;
    @Autowired
    private JbPackageOfflinePriceController jbPackageOfflinePriceController;
    @Autowired
    private JbPackageFactory jbPackageFactory;

    /**
     * 查询套餐列表
     */
    @ApiOperation("查询套餐列表")
    @PreAuthorize("@ss.hasPermi('package_main:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbPackageMain jbPackageMain)
    {
        return JoR.rows(jbPackageFactory.list(page, jbPackageMain));
    }

    /**
     * 导出套餐列表
     */
    @ApiOperation("导出套餐列表")
    @PreAuthorize("@ss.hasPermi('package_main:export')")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbPackageMain jbPackageMain)
    {
        LambdaQueryWrapper<JbPackageMain> select = new LambdaQueryWrapper<>();
        IPage<JbPackageMain> iPage = service.page(page.b(), select);

        List<JbPackageMain> list = iPage.getRecords();
        ExcelUtil<JbPackageMain> util = new ExcelUtil<JbPackageMain>(JbPackageMain.class);
        util.exportExcel(response, list, "套餐数据");
    }

    /**
     * 获取套餐详细信息
     */
    @ApiOperation("获取套餐详细信息")
    @PreAuthorize("@ss.hasPermi('package_main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        JbPackageMain jbPackageMain = service.getById(id);
        jbPackageMain.setCommodityList(jbPackageCommodityController.listAll(id));
        jbPackageMain.setOfflinePriceList(jbPackageOfflinePriceController.listAll(id));
        return success(jbPackageMain);
    }

    /**
     * 新增套餐
     */
    @ApiOperation("新增套餐")
    @PreAuthorize("@ss.hasPermi('package_main:add')")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbPackageMain jbPackageMain)
    {
        if (!service.save(jbPackageMain))
        {
            return error();
        }
        if (!jbPackageCommodityController.adds(jbPackageMain.getCommodityList(), jbPackageMain.getId()))
        {
            return error();
        }

        return success(jbPackageOfflinePriceController.adds(jbPackageMain.getOfflinePriceList(),
                jbPackageMain.getId()));
    }

    /**
     * 修改套餐状态
     */
    @ApiOperation("修改套餐状态")
    @PreAuthorize("@ss.hasPermi('package_main:isStatus')")
    @Log(title = "修改套餐状态", businessType = BusinessType.UPDATE)
    @PutMapping("/isStatus")
    public AjaxResult isStatus(@RequestBody JbPackageMain jbPackageMain)
    {
        LambdaUpdateWrapper<JbPackageMain> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JbPackageMain::getId, jbPackageMain.getId());
        updateWrapper.set(JbPackageMain::getStatus, jbPackageMain.getStatus());

        return toAjax(service.update(updateWrapper));
    }

    /**
     * 修改套餐
     */
    @ApiOperation("修改套餐")
    @PreAuthorize("@ss.hasPermi('package_main:edit')")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbPackageMain jbPackageMain)
    {
        if (!service.updateById(jbPackageMain))
        {
            return error();
        }

        if (!jbPackageCommodityController.edits(jbPackageMain.getCommodityList(), jbPackageMain.getId()))
        {
            return error();
        }

        return toAjax(jbPackageOfflinePriceController.edits(jbPackageMain.getOfflinePriceList(),
                jbPackageMain.getId()));
    }

    /**
     * 删除套餐
     */
    @ApiOperation("删除套餐")
    @PreAuthorize("@ss.hasPermi('package_main:remove')")
    @Log(title = "套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
}
